#include <iostream>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t gmutex = PTHREAD_MUTEX_INITIALIZER;//锁
pthread_cond_t gcond = PTHREAD_COND_INITIALIZER;//条件变量
int data = 10;
void *Visit(void *arg)
{
    while (true)
    {
        pthread_mutex_lock(&gmutex);
        pthread_cond_wait(&gcond, &gmutex);//等待被唤醒
        std::cout<<(char*)arg<<"被唤醒"<<std::endl;
        std::cout << "data: " << data++ << std::endl;
        pthread_mutex_unlock(&gmutex);
    }

    return nullptr;
}

int main()
{
    pthread_t tid1;
    pthread_create(&tid1, nullptr, Visit, (void *)"thread-1");
    pthread_t tid2;
    pthread_create(&tid2, nullptr, Visit, (void *)"thread-2");
    pthread_t tid3;
    pthread_create(&tid3, nullptr, Visit, (void *)"thread-3");
    while (true)
    {
        // std::cout << "唤醒一个线程" << std::endl;
        // pthread_cond_signal(&gcond);//唤醒一个线程
        // sleep(1);
        std::cout<<"唤醒所有进程"<<std::endl;
        pthread_cond_broadcast(&gcond);
        sleep(2);
    }
    return 0;
}